package com.sz.common.base.i18n;

import com.sz.common.base.utils.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * Package: com.sz.common.base.i18n <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-05-20 16:51:00.
 */
public class I18nMessageHelper {

    private static MessageSource messageSource = null;

    /**
     * @param key  message key
     * @param args args
     * @return
     */
    public static String getI18nMessage(String key, Object[] args) {
        try {
            return getMessageSource().getMessage(key, args, LocaleContextHolder.getLocale());
        } catch (Exception ex) {
            String defaultMessage = "message key [" + key + "] not exist";
            return defaultMessage;
        }
    }

    private static MessageSource getMessageSource() {
        if (messageSource == null) {
            messageSource = BeanUtils.getBean(MessageSource.class); // new ResourceBundleMessageSource();
        }
        return messageSource;
    }
}
